const Mock = require('mockjs')

// 探索模块 获取作品预览接口 测试数据
var { exploreList } = Mock.mock({
  'exploreList|10': [
  // 'exploreList|1': [
    {
      workId: '@increment()',
      workName: '@ctitle()',
      workTime: '@date(yyyy-MM-dd)',
      'workType|1': ['文档', '图像'],
      workDescription: '@cparagraph(5,15)',
      workLabels: ['风景', '学术', '创意', '设计'],
      'workStatus|1': ['审核中', '已通过', '未通过', '申诉中'],
      workAddress: '@image("100x100","#999","#fff","png","api_test")',
      authorName: '@cname()'
    }
  ]
})

// 用户主页模块 获取作品预览接口 测试数据
var { homeWorkList } = Mock.mock({
  'homeWorkList|10': [
    {
      authorName: '测试用户',
      workId: '@increment()',
      workName: '@ctitle()',
      workTime: '@date(yyyy-MM-dd)',
      'workType|1': ['文档', '图像'],
      workDescription: '@cparagraph(5,15)',
      workLabels: ['风景', '学术', '创意', '设计'],
      'workStatus|1': ['审核中', '已通过', '未通过', '申诉中'],
      workAddress: '@image("100x100","#999","#fff","png","api_test")'
    }
  ]
})

// 管理员模块 获取待审核作品预览接口 测试数据
var { checkWorkList } = Mock.mock({
  'checkWorkList|10-20': [
    {
      workId: '@increment()',
      workName: '@ctitle()',
      authorName: '@cname()',
      workDescription: '@cparagraph(5,15)',
      workTime: '@date(yyyy-MM-dd)',
      'workType|1': ['文档', '图像'],
      'workStatus|1': ['审核中', '已通过', '未通过', '申诉中'],
      workLabels: ['风景', '学术', '创意', '设计'],
      workAddress: '@image("100x100","#999","#fff","png","api_test")'
    }
  ]
})

// 管理员模块 获取待处理申诉预览接口 测试数据
var { checkAppealList } = Mock.mock({
  'checkAppealList|10-20': [
    {
      workId: '@increment()',
      workName: '@ctitle()',
      authorName: '@cname()',
      workDescription: '@cparagraph(5,15)',
      workTime: '@date(yyyy-MM-dd)',
      'workType|1': ['文档', '图像'],
      // 'appealStatus|1': ['已处理', '待处理'],
      workStatus: '申诉中',
      appealReason: '@cparagraph(5,15)',
      appealTime: '@date(yyyy-MM-dd)',
      workAddress: '@image("100x100","#999","#fff","png","api_test")'
    }
  ]
})

// 管理员模块 作品detail接口 测试数据
var { workDetail } = Mock.mock({
  'workDetail|1': [
    {
      workId: '@increment()',
      workName: '@ctitle()',
      authorName: '@cname()',
      userType: '普通用户',
      workDescription: '@cparagraph(5,15)',
      workTime: '@date(yyyy-MM-dd)',
      'workType|1': ['文档', '图像'],
      workLabels: ['风景', '学术', '创意', '设计'],
      workAddress: '@image("100x100","#999","#fff","png","api_test")'
    }
  ]
})

// 管理员模块 申诉detail接口 测试数据
var { appealDetail } = Mock.mock({
  'appealDetail|1': [
    {
      workId: '@increment()',
      workName: '@ctitle()',
      authorName: '@cname()',
      appealDescription: '@cparagraph(5,15)',
      appealTime: '@date(yyyy-MM-dd)',
      'workType|1': ['文档', '图像'],
      // 'appealStatus|1': ['已处理', '待处理'],
      workStatus: '申诉中',
      workAddress: '@image("100x100","#999","#fff","png","api_test")'
    }
  ]
})

let EList = []
EList = exploreList

let HWList = []
HWList = homeWorkList

let CWList = []
CWList = checkWorkList

let CAList = []
CAList = checkAppealList

// 获取url参数
const getQuery = (url, name) => {
  const index = url.indexOf('?')
  if (index !== -1) {
    const queryStrArr = url.substr(index + 1).split('&')
    for (var i = 0; i < queryStrArr.length; i++) {
      const itemArr = queryStrArr[i].split('=')
      // console.log(itemArr)
      if (itemArr[0] === name) {
        return itemArr[1]
      }
    }
  }
  return null
}

// 定义Explore页面获取其他作品列表数据的接口
Mock.mock(/\/user\/getWorkList/, 'get', (options) => {
  // console.log(options)
  const pageindex = getQuery(options.url, 'pageindex')
  const pagesize = getQuery(options.url, 'pagesize')
  console.log(pageindex, pagesize)
  const start = (pageindex - 1) * pagesize
  const end = pageindex * pagesize
  // const totalPage = Math.ceil(exploreList.length / pagesize)
  // // （待修改）
  // const list = pageindex > totalPage ? [] : exploreList.slice(start, end)
  const EListForDetail = EList
  const totalPage = Math.ceil(EListForDetail.length / pagesize)
  const list = pageindex > totalPage ? [] : EListForDetail.slice(start, end)
  return {
    data: list,
    total: EList.length
  }
})

// 定义homework页面获取作品列表数据的接口
Mock.mock(/\/user\/getHomeWorkList/, 'get', (options) => {
  console.log(options) // userID, pageindex, pagesize
  const pageindex = getQuery(options.url, 'pageindex')
  const pagesize = getQuery(options.url, 'pagesize')
  console.log(pageindex, pagesize)
  const start = (pageindex - 1) * pagesize
  const end = pageindex * pagesize
  // const totalPage = Math.ceil(homeWorkList.length / pagesize)
  // const list = pageindex > totalPage ? [] : homeWorkList.slice(start, end)
  const hwListForDelete = HWList
  const totalPage = Math.ceil(hwListForDelete.length / pagesize)
  const list = pageindex > totalPage ? [] : hwListForDelete.slice(start, end)

  return {
    data: list,
    total: HWList.length
  }
})

// 定义主页作品申诉接口
Mock.mock(/\/user\/postAppeal/, 'post', (options) => {
  console.log(options) // userID, pageindex, pagesize
  const workId = JSON.parse(options.body).params.workId
  const workName = JSON.parse(options.body).params.workName
  const userId = JSON.parse(options.body).params.userId
  const reason = JSON.parse(options.body).params.reason
  console.log('workId:', workId)
  console.log('userId:', userId)
  console.log('workName:', workName)
  console.log('reason:', reason)

  if (!reason) {
    return {
      data: false,
      msg: '未填写申诉理由'
    }
  } else if (!workId) {
    return {
      data: false,
      msg: '参数错误，未能提交申诉'
    }
  } else {
    HWList.filter(u => u.workId === workId)[0].workStatus = '申诉中'
    return {
      data: true,
      msg: '成功提交申诉'
    }
  }
})

// 定义主页作品删除接口
Mock.mock(/\/user\/workDelete/, 'post', (options) => {
  console.log(options) // userID, pageindex, pagesize
  const id = JSON.parse(options.body).params.workId
  console.log('id:', id)
  if (!id) {
    return {
      data: false,
      msg: '参数不正确'
    }
  } else {
    HWList = HWList.filter(u => u.workId !== id)
    // console.log(List)
    return {
      data: true,
      msg: '删除成功'
    }
  }
})

// 定义主页模块 作品详情的接口
Mock.mock(/\/user\/getImg/, 'get', (options) => {
  console.log(options)
  const fileId = getQuery(options.url, 'fileId')
  console.log(fileId)

  if (fileId) {
    // workDetail = EList.filter(u => u.workId.toString() === fileId)[0]
    workDetail = HWList.filter(u => u.workId.toString() === fileId)[0]
    console.log(workDetail)
    return {
      code: true,
      base64: '',
      data: workDetail,
      msg: '获取作品详情成功'
    }
  } else {
    return {
      code: false,
      base64: '',
      data: [],
      msg: '获取作品详情失败'
    }
  }
})

// 定义管理员模块 待审核作品页面获取作品列表数据的接口
Mock.mock(/\/admin\/getCheckWorkList/, 'get', (options) => {
  console.log(options) // userID, pageindex, pagesize
  const pageindex = getQuery(options.url, 'pageindex')
  const pagesize = getQuery(options.url, 'pagesize')
  console.log(pageindex, pagesize)
  const start = (pageindex - 1) * pagesize
  const end = pageindex * pagesize
  // const totalPage = Math.ceil(checkWorkList.length / pagesize)
  // const list = pageindex > totalPage ? [] : checkWorkList.slice(start, end)
  const cwListForDetail = CWList
  const totalPage = Math.ceil(cwListForDetail.length / pagesize)
  const list = pageindex > totalPage ? [] : cwListForDetail.slice(start, end)
  return {
    data: list,
    total: CWList.length
  }
})

// 定义管理员模块 待处理申诉页面获取作品列表数据的接口
Mock.mock(/\/admin\/getCheckAppealList/, 'get', (options) => {
  console.log(options) // pageindex, pagesize
  const pageindex = getQuery(options.url, 'pageindex')
  const pagesize = getQuery(options.url, 'pagesize')
  console.log(pageindex, pagesize)
  const start = (pageindex - 1) * pagesize
  const end = pageindex * pagesize
  // const totalPage = Math.ceil(checkAppealList.length / pagesize)
  // const list = pageindex > totalPage ? [] : checkAppealList.slice(start, end)
  const caListForDetail = CAList
  const totalPage = Math.ceil(caListForDetail.length / pagesize)
  const list = pageindex > totalPage ? [] : caListForDetail.slice(start, end)
  return {
    data: list,
    total: CAList.length
  }
})

// 定义管理员模块 获取审核作品
Mock.mock(/\/admin\/getWork/, 'get', (options) => {
  console.log(options)
  const fileId = getQuery(options.url, 'fileId')
  console.log(fileId)
  workDetail = CWList.filter(u => u.workId.toString() === fileId)[0]
  return {
    msg: '获取作品详情成功',
    data: workDetail
  }
  // if (fileId) {
  //   workDetail = List.filter(u => u.workId.toString() === fileId)[0]
  //   return {
  //     code: true,
  //     message: '获取作品详情成功',
  //     data: workDetail
  //   }
  // } else {
  //   return {
  //     code: false,
  //     message: '获取作品详情失败',
  //     data: []
  //   }
  // }
})

// 管理员模块 改变作品审核状态
Mock.mock(/\/admin\/changeWorkStatus/, 'post', (options) => {
  console.log(options)
  const fileId = JSON.parse(options.body).params.fileId
  const formReason = JSON.parse(options.body).params.formReason
  const formAdvice = JSON.parse(options.body).params.formAdvice
  console.log('workId:', fileId)
  console.log('reason:', formReason)
  console.log('advice:', formAdvice)

  if (fileId) {
    workDetail = CWList.filter(u => u.workId.toString() === fileId)[0]
    if (formReason.length) {
      workDetail.workStatus = '未通过'
    } else {
      workDetail.workStatus = '已通过'
    }
    return {
      msg: '作品审核状态更改成功',
      data: {
        code: true,
        data: workDetail
      }
    }
  } else {
    return {
      msg: '作品审核状态更改失败',
      data: {
        code: false,
        data: workDetail
      }
    }
  }
})

// 定义管理员模块 申诉
Mock.mock(/\/admin\/getAppeal/, 'get', (options) => {
  console.log(options)
  const fileId = getQuery(options.url, 'fileId')
  console.log(fileId)
  appealDetail = CAList.filter(u => u.workId.toString() === fileId)[0]
  return {
    msg: '获取申诉详情成功',
    data: appealDetail
  }
})

// 管理员模块 改变作品申诉状态
Mock.mock(/\/admin\/changeAppealStatus/, 'post', (options) => {
  console.log(options)
  const fileId = JSON.parse(options.body).params.fileId
  const formReason = JSON.parse(options.body).params.formReason
  const formAdvice = JSON.parse(options.body).params.formAdvice
  console.log('workId:', fileId)
  console.log('reason:', formReason)
  console.log('advice:', formAdvice)

  if (fileId) {
    appealDetail = CAList.filter(u => u.workId.toString() === fileId)[0]
    // appealDetail.appealStatus = '已处理'
    if (formReason.length) {
      appealDetail.workStatus = '未通过'
    } else {
      appealDetail.workStatus = '已通过'
    }
    return {
      msg: '作品申诉状态更改成功',
      data: {
        code: true,
        data: appealDetail
      }
    }
  } else {
    return {
      msg: '作品申诉状态更改失败',
      data: {
        code: false,
        data: appealDetail
      }
    }
  }
})
